/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication3;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Morgan
 */
public class SolidscapePref extends javax.swing.JFrame {
    SQLMethods dba = new SQLMethods();
    /**
     * Creates new form SolidScapePref
     */
    public void SolidscapePrefStart() {
        initComponents();
        curPrice.setText("0");
        newPrice1.setText("0");
        ResultSet res = dba.searchPrinterSettings("Solidscape");
        try {
            if (res.next()) {
                curPrice.setText(res.getString("materialCostPerUnit"));
            } else {
                curPrice.setText("0");
            }
        } catch (SQLException ex) {
            Logger.getLogger(SolidscapePref.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SolidscapePref.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        setVisible(true);
    }

    public void getPrice() {
        float CostPerHour = Float.parseFloat(newPrice1.getText());
        if (Float.parseFloat(newPrice1.getText()) != 0) {
            //Update
            dba.updatePrinterSettings("Solidscape", "Time", "Minutes", CostPerHour, "", "", 0, "", "", 0, "", "", 0, "", "", 0);
            System.out.println("Updated Price Settings");
        } else {
            //create new printer
            dba.insertPrinterSettings("Solidscape", "Time", "Minutes", CostPerHour, "", "", 0, "", "", 0, "", "", 0, "", "", 0);
            System.out.println("Created New Price Settings");
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        submit = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        curPrice = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        newPrice1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Preferences");
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        submit.setText("Confirm Changes");
        submit.setBorder(null);
        submit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                submitActionPerformed(evt);
            }
        });
        getContentPane().add(submit, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 120, 120, 20));

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Current Price per hour: $");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 60, 120, 20));

        curPrice.setEditable(false);
        getContentPane().add(curPrice, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 60, 190, -1));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Solidscape Pricing Settings");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, 30));
        getContentPane().add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, 340, 10));
        getContentPane().add(newPrice1, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 90, 190, -1));

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("New Price per hour: $");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 90, 110, 20));

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void submitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitActionPerformed
        // Update database with new price if it is a valid number
        getPrice();
        dispose();
    }//GEN-LAST:event_submitActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField curPrice;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField newPrice1;
    private javax.swing.JButton submit;
    // End of variables declaration//GEN-END:variables
}
